import {
  Controller,
  Post,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { join } from 'path';

@Controller()
export class Upload {
  @Post('upload')
  @UseInterceptors(FileInterceptor('file')) // 'file'是前端传递文件时的字段名
  async uploadFile(@UploadedFile() file: Express.Multer.File) {
    const filename = new Date().getTime() + file.originalname;
    fs.writeFileSync(
      join(__dirname, '../..', 'uploads/image', filename),
      file.buffer,
    );
    return {
      originalname: file.originalname,
      filename: '/image/' + filename,
    };
  }
}
